﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Controls;

namespace StudentManagement.Util.ValidationRules
{
    public class MaxNumericalValidation : ValidationRule
    {
        private readonly decimal _maxNumerical;
        private readonly string _errorMsg;

        /// <summary>
        /// 验证最大值
        /// </summary>
        /// <param name="maxNumerical">最大值</param>
        public MaxNumericalValidation(decimal maxNumerical, string errorMsg)
        {
            _maxNumerical = maxNumerical;
            _errorMsg = errorMsg;
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value != null && value != (object)string.Empty)
            {
                if (decimal.TryParse(value.ToString(), out decimal valueNum))
                {
                    if (valueNum > _maxNumerical)
                    {
                        return new ValidationResult(false, _errorMsg);
                    }
                }
            }
            return ValidationResult.ValidResult;
        }
    }
}
